package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.custom.Vo.UserInfoVo;
import com.future.hair.server.entity.*;
import com.future.hair.server.mapper.*;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

@RestController
@RequestMapping("//hair/home")
@Api(tags = "home 首页接口")
public class HomeController {

    @Autowired
    private MallMapper mapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BarberAppraiseMapper barberAppraiseMapper;

    @Autowired
    private UserOrderMapper orderMapper;

    @Autowired
    private WorksMapper worksMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /// 查询门店
    @ApiOperation(value = "查询 Mall 店面 列表")
    @RequestMapping(value = "/mallList", method = RequestMethod.POST)
    public CommonResult<List<Mall>> mallList(@RequestBody PageModel data) {
        return CommonResult.data(mapper.selectPage(new Page<>(data.getCurrent(), data.getSize()), null).getRecords());
    }

    /// 查询店员详情
    @ApiOperation(value = "查询 Mall 店面 详情")
    @RequestMapping(value = "/mallDetail", method = RequestMethod.POST)
    public CommonResult<Mall> mallDetail(@RequestBody Mall data) {
        return CommonResult.data(mapper.selectById(data.getId()));
    }

    /// 查询店员列表
    @ApiOperation(value = "查询 barber 店员 列表")
    @RequestMapping(value = "/barberList", method = RequestMethod.POST)
    public CommonResult<List<UserInfoVo>> barberList(@RequestBody PageModel data) {
        List<UserInfoVo> list = new ArrayList<UserInfoVo>();
        QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
        infoWrapper.eq("mall_id",data.getMallId());
        List<UserInfo> userInfoList = infoMapper.selectPage(new Page<>(data.getCurrent(), data.getSize()), infoWrapper).getRecords();
        for (UserInfo info :
                userInfoList) {
            list.add(getUserInfoVo(info));
        }
        return CommonResult.data(list);
    }

    /// 查询店员详情
    @ApiOperation(value = "查询 barber 店员 详情")
    @RequestMapping(value = "/barberDetail", method = RequestMethod.POST)
    public CommonResult<UserInfoVo> barberDetail(@RequestBody UserInfo data) {
        return CommonResult.data(getUserInfoVo(infoMapper.selectById(data.getId())));
    }

    /// 查询用户信息
    @ApiOperation(value = "查询 用户 详情")
    @RequestMapping(value = "/userInfoDetail", method = RequestMethod.POST)
    public CommonResult<UserInfo> userInfoDetail(@RequestHeader String token) {
        QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
        infoWrapper.eq("user_id", JwtHelper.getByUserId(token));
        return CommonResult.data(infoMapper.selectOne(infoWrapper));
    }

    public UserInfoVo getUserInfoVo(UserInfo data){
        if (null==data){
            return null;
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserInfo(data);
//        userInfoVo.setUser(userMapper.selectById(userInfo.getUserId()));
        userInfoVo.setMall(mapper.selectById(data.getMallId()));

        QueryWrapper<BarberAppraise> barberAppraiseWrapper = new QueryWrapper<BarberAppraise>();
        barberAppraiseWrapper.eq("barber_id", data.getId());
        userInfoVo.setAppraiseCount(barberAppraiseMapper.selectCount(barberAppraiseWrapper));
        barberAppraiseWrapper.eq("type", 1);
        userInfoVo.setGoodAppraiseCount(barberAppraiseMapper.selectCount(barberAppraiseWrapper));

        QueryWrapper<UserOrder> orderWrapper = new QueryWrapper<UserOrder>();
        userInfoVo.setReservesCount(orderMapper.selectCount(null));
        long current1 = System.currentTimeMillis();
        long zero1 = current1/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        orderWrapper.ge("time", zero1);
        long current2 = System.currentTimeMillis() + (1000 * 60 * 60 * 24);
        long zero2 = current2/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        orderWrapper.le("time", zero2);
        userInfoVo.setTodayReservesCount(orderMapper.selectCount(orderWrapper));


        QueryWrapper<Works> worksWrapper = new QueryWrapper<Works>();
        worksWrapper.eq("user_info_id",data.getId());
        List<Works> worksList = worksMapper.selectPage(new Page<>(1, 10), worksWrapper).getRecords();
        userInfoVo.setImgList(worksList.stream().map(Works::getCover).collect(Collectors.toList()));
        return userInfoVo;
    }

}
